const { genTestUserSig } = require('../../debug/GenerateTestUserSig')

Page({

    /**
     * 页面的初始数据
     */
    data: {
        // 通话内容那个介绍
        entryInfos: [
            { icon: 'https://web.sdk.qcloud.com/component/miniApp/resources/audio-card.png', title: '语音通话', desc: '丢包率70%仍可正常语音通话', type: 1 },
            { icon: 'https://web.sdk.qcloud.com/component/miniApp/resources/video-card.png', title: '视频通话', desc: '丢包率50%仍可正常视频通话', type: 2 },
        ],
        // 通话配置信息
        config: {
            sdkAppID: 1400693832,
            userID: '',
            userSig: '',
            type: 2, // 1 语音 2 视频
            tim: null,
        },
        animationData: {}, // animation动画数据
        searchUserID: '', // 搜索的userId
        invitee: {
          userID: ''
        }, // 邀请人对象
        windowHeight: wx.getSystemInfoSync().windowHeight
    },
    TUICalling: {}, // 通信器
    onLoad () {
      this.TUICalling = this.selectComponent('#TUICalling-component')
    },
    // 修改用户ID
    changeUserID (e: any) {
        const { value } = e.detail;
        this.setData({
            'config.userID': value,
        })
    },
    // 显示搜索弹窗
    showDialog (e: any) {
      const { type } = e.currentTarget.dataset;
      const Signature = genTestUserSig(this.data.config.userID)
      const animation = wx.createAnimation({
        duration: 300,
        timingFunction: 'linear'
      })
      animation.translateY(0).step()
      this.setData({
        'config.type': type,
        animationData: animation.export(),
        'config.userSig': Signature.userSig
      });
      // 初始化通信器
      (this.TUICalling as any).init()
    },
    // 改变搜索用户id
    changeSearchUserId (e: any) {
      this.setData({
        searchUserID: e.detail.value
      })
    },
    // 搜索用户
    searchUser () {
      this.setData({
        'invitee.userID': this.data.searchUserID
      })
    },
    // 呼叫函数
    call () {
      if (this.data.config.userID === this.data.invitee.userID) {
        wx.showToast({
          icon: 'none',
          title: '不可呼叫本机',
        })
        return
      };
      (this.TUICalling as any).call({ userID: this.data.invitee.userID, type: this.data.config.type })
    },
    // 关闭搜索弹窗
    closeDialog () {
      const animation = wx.createAnimation({
        duration: 300,
        timingFunction: 'linear'
      })
      animation.translateY(this.data.windowHeight).step()
      this.setData({
        animationData: animation.export(),
        searchUserID: '',
        'invitee.userID': ''
      });
    }
})